$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'footprint/vipfootprintinfos/list',
        datatype: "json",
        colModel: [			
			{ label: '足迹ID', name: 'vipfootprintinfoid', index: 'VipFootprintInfoId', width: 50, key: true },
			{ label: '项目名', name: 'projectinfo.projectname', index: 'projectname', width: 80 },
			{ label: '项目类型', name: 'projectinfo.projecttype', index: 'projecttype', width: 80,formatter: function(value, options, row){
                    if(value == null){
                        return "";
                    }
                    if(value == 0){
                        return "工程";
                    }else if(value == 1){
                        return "车队";
                    }else {
                        return "错误！";
                    }
                }},
			{ label: '用户', name: 'userinfo.useraccount', index: 'useraccount', width: 80 },
			{ label: '创建时间', name: 'createtime', index: 'CreateTime', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        //选择项目界面项目查询参数
        selectProjectSearchParam:null,
        //选择用户界面用户查询参数
        selectUserSearchParam:null,
        //主界面搜索框查询参数
        searchParam:'',
        //项目类型查询
        searchType:'',
        //判断当前按钮执行的什么操作的标识（新增，修改，等）
		btn:'index',
        //保存选择界面的名称
        projectinfo:{"projectname":''},
        userinfo:{"useraccount":''},
		showList: true,
		title: null,
        //足迹信息
		vipfootprintinfos: {},
        //用户界面保存用户信息
        users:{},
        //用户界面是否显示
        usershow:true,
        //用户查询的参数
        username:'',
        //每页条数
        userlimit:9,
        //排序
        order:"asc"
	},
    mounted: function(){
        getUserList({username:this.username,limit:this.userlimit,page:1,order:this.order})
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			// vm.vipfootprintinfos = {};
            vm.btn = 'add';
        },
		update: function (event) {
			var  vipfootprintinfoid = getSelectedRow();
			if( vipfootprintinfoid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.btn = 'update';
            vm.getInfo( vipfootprintinfoid);
		},
		saveOrUpdate: function (event) {
			var url = vm.vipfootprintinfos. vipfootprintinfoid == null ? "footprint/vipfootprintinfos/save" : "footprint/vipfootprintinfos/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.vipfootprintinfos),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
							//清空选项中的数据
                            vm.vipfootprintinfos = {};
                            vm.projectinfo = {projectname:''};
                            vm.userinfo={useraccount:''};
                            vm.btn = 'index';
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var  vipfootprintinfoids = getSelectedRows();
			if( vipfootprintinfoids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "footprint/vipfootprintinfos/delete",
                    contentType: "application/json",
				    data: JSON.stringify( vipfootprintinfoids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(vipfootprintinfoid){
			$.get(baseURL + "footprint/vipfootprintinfos/info/"+ vipfootprintinfoid, function(r){
                vm.vipfootprintinfos = {"vipfootprintinfoid":r.vipfootprintinfos.vipfootprintinfoid,
                                            "createtime":r.vipfootprintinfos.createtime,
                                            "targetid":r.vipfootprintinfos.projectinfo.projectinfoid,
                                            "userinfoid":r.vipfootprintinfos.userinfo.userinfoid};
                //设置用户项目信息
                vm.userinfo = r.vipfootprintinfos.userinfo;
                vm.projectinfo = r.vipfootprintinfos.projectinfo;
            });
		},
		reload: function (event) {
            vm.vipfootprintinfos = {};
            vm.projectinfo = {projectname:''};
            vm.userinfo={useraccount:''};
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    searchParam:vm.searchParam,
                    "projecttype":vm.searchType
                }
            }).trigger("reloadGrid");
			vm.vipfootprintinfos = {};
			vm.userinfo = {"useraccount":''};
			vm.projectinfo = {"projectname":''};
            editor.txt.clear();
			vm.btn = 'index';
		},
        queryuser: function(){
            getUserList({username:this.username,limit:this.userlimit,page:1,order:this.order})
        },
        queryfootprintbyuser: function(event){
            var target = event.currentTarget;
            var all = document.getElementById("all");
			//点击全部用户时，更新数据时，顺便显示所有用户
			if(target === all){
				this.username = '';
                getUserList({username:this.username,limit:this.userlimit,page:1,order:this.order})
			}

            var value = target.getAttribute('value');
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{userinfoid:value}
            }).trigger("reloadGrid");
            showList = true;
            return false;
        },
        ushow: function(){
            if (this.usershow) {
                $("#jqGrid").setGridWidth($(window).width()*0.98);
                $("#desc").width($(window).width()*0.98);
                this.usershow = false;
            } else {
                $("#jqGrid").setGridWidth($(window).width()*0.8);
                $("#desc").width($(window).width()*0.8);
                this.usershow = true;

            }
        },
        previous:function(){
            previous({username:this.username,limit:this.userlimit,page:(this.users.currPage-1),order:this.order});
        },
        next: function(){
            next({username:this.username,limit:this.userlimit,page:(this.users.currPage+1),order:this.order});
        },
        selectUser:function () {
            //新增才能选择用户
            // if (vm.btn == 'add'){
                //弹窗数据请求的url
                var selectUrl = 'user/userinfos/list';
                //要显示的弹窗数据列
                var tableList = [
                    { label: '用户ID', name: 'userinfoid', index: 'UserInfoId', width: 80, key: true },
                    { label: '用户账户', name: 'useraccount', index: 'UserAccount', width: 60 },
                    { label: '用户名', name: 'username', index: 'UserName', width: 60 },
                    { label: '性别', name: 'usergender', index: 'UserGender',align:"center", width: 20,formatter: function(value, options, row){
                            return value === 0 ? '男' : '女';
                        } },
                    { label: '手机号码', name: 'phonenum', index: 'PhoneNum', width: 80 },
                    { label: '是否Vip', name: 'isvip', index: 'IsVip', width: 40 ,formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-info">普通用户</span>' :
                                '<span class="label label-primary">VIP用户</span>';
                        }}
                ];
                window.T.selectLayer(selectUrl,tableList,"selectLayer","selectLayerPager");
                layer.open({
                    type: 1,
                    offset: '50px',
                    skin: 'layui-layer-molv',
                    title: "选择用户",
                    area: ['67%', '85%'],
                    shade: 0,
                    shadeClose: false,
                    content: jQuery("#selectUserID"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var row = window.T.getSelectedRow("selectLayer");
                        if (row != null){
                            //设置显示值
                            vm.userinfo = row;
                            //设置请求值
                            vm.vipfootprintinfos.userinfoid = vm.userinfo.userinfoid;
                        }
                        layer.close(index);
                    }
                });
            // }
        },
        selectUserQuery:function () {
            var page = $("#selectLayer").jqGrid('getGridParam','page');
            $("#selectLayer").jqGrid('setGridParam',{
                url:baseURL + "user/userinfos/queryUser" + ((vm.selectUserSearchParam != null)? "?searchParam=" + vm.selectUserSearchParam:""),
                page:page
            }).trigger("reloadGrid");
            vm.selectUserSearchParam = null;
        },
        selectProject:function(){
            //新增才能选择用户
            // if (vm.btn == 'add'){
                //弹窗数据请求的url
                var selectUrl = 'projectinfo/projectinfos/desclist';
                //要显示的弹窗数据列
                var tableList = [
                    { label: '项目ID', name: 'projectinfoid', index: 'ProjectInfoId', width: 60, key: true },
                    { label: '地区', name: 'areaname', index: 'AreaInfoId', width: 65 },
                    { label: '发布者', name: 'useraccount', index: 'CreateUser', width: 80 },
                    { label: '项目名称', name: 'projectname', index: 'ProjectName', width: 80 },
                    { label: '项目类型', name: 'projecttype', index: 'ProjectType', width: 80 ,formatter: function(value, options, row){
                            if(value == null){
                                return "";
                            }
                            if(value == 0){
                                return "工程";
                            }else if(value == 1){
                                return "车队";
                            }else {
                                return "错误！";
                            }
                        }}
                ];
                window.T.selectLayer(selectUrl,tableList,"selectProjectLayer","selectProjectLayerPager");
                layer.open({
                    type: 1,
                    offset: '50px',
                    skin: 'layui-layer-molv',
                    title: "选择用户",
                    area: ['67%', '85%'],
                    shade: 0,
                    shadeClose: false,
                    content: jQuery("#selectProjectID"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var row = window.T.getSelectedRow("selectProjectLayer");
                        if (row != null){
                            //设置显示值
                            vm.projectinfo = row;
                            //设置请求值
                            vm.vipfootprintinfos.targetid = vm.projectinfo.projectinfoid;
                        }
                        layer.close(index);
                    }
                });
            // }
        },
        selectProjectQuery:function(){
            var page = $("#selectProjectLayerPager").jqGrid('getGridParam','page');
            $("#selectProjectLayer").jqGrid('setGridParam',{
                url:baseURL + "projectinfo/projectinfos/desclist",
                page:page,
                postData: {"searchParam": vm.selectProjectSearchParam},
            }).trigger("reloadGrid");
            vm.selectUserSearchParam = null;
        }
	}
});